package org.jeecg.modules.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flow.entity.FlowProcessBusiness;
import org.jeecg.modules.flow.service.IFlowProcessBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: flow_process_business
 * @Author: jeecg-boot
 * @Date: 2021-11-22
 * @Version: V1.0
 */
@Api(tags = "流程匹配管理")
@RestController
@RequestMapping("/flow/adaptive")
@Slf4j
public class FlowProcessBusinessController extends JeecgController<FlowProcessBusiness, IFlowProcessBusinessService> {
    @Autowired
    private IFlowProcessBusinessService flowProcessBusinessService;

    /**
     * 分页列表查询
     *
     * @param flowProcessBusiness
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */

    @ApiOperation(value = "分页列表查询", notes = "flow_process_business-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FlowProcessBusiness flowProcessBusiness,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FlowProcessBusiness> queryWrapper = QueryGenerator.initQueryWrapper(flowProcessBusiness, req.getParameterMap());
        Page<FlowProcessBusiness> page = new Page<FlowProcessBusiness>(pageNo, pageSize);
        IPage<FlowProcessBusiness> pageList = flowProcessBusinessService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据企业号、机构号、业务种类获取流程
     *
     * @param
     * @return
     */
    @ApiOperation(value = "匹配流程", notes = "flow_process_business-匹配流程")
    @GetMapping(value = "/match")
    public String queryMatch(Integer cusNo, String orgId, String businessType) {

        String processId = flowProcessBusinessService.matchProcess(cusNo, orgId, businessType);

        return processId;
    }


    /**
     * 添加
     *
     * @param flowProcessBusiness
     * @return
     */
    @ApiOperation(value = "添加匹配关系", notes = "flow_process_business-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FlowProcessBusiness flowProcessBusiness) {
        flowProcessBusinessService.save(flowProcessBusiness);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param flowProcessBusiness
     * @return
     */
    @ApiOperation(value = "修改匹配关系", notes = "flow_process_business-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FlowProcessBusiness flowProcessBusiness) {
        flowProcessBusinessService.updateById(flowProcessBusiness);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除匹配关系", notes = "flow_process_business-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        flowProcessBusinessService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除匹配关系", notes = "flow_process_business-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.flowProcessBusinessService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询匹配关系", notes = "flow_process_business-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FlowProcessBusiness flowProcessBusiness = flowProcessBusinessService.getById(id);
        if (flowProcessBusiness == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(flowProcessBusiness);
    }

}
